package com.apex.crm.interceptors;

import com.apex.crm.exceptions.NoLoginException;
import com.apex.crm.service.UserService;
import com.apex.crm.utils.LoginUserUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname NoLoginInterceptor
 * @description TODO
 * @date 2021/4/21 21:29
 */

//非法访问拦截

public class NoLoginInterceptor extends HandlerInterceptorAdapter {

    @Resource
    private UserService userService;

    //判断用户是否是登录状态  获取Cookie  解析ID
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
        Integer userId= LoginUserUtil.releaseUserIdFromCookie(request);
        if(null==userId||null==userService.selectByPrimaryKey(userId)){
            throw  new NoLoginException();
        }
        return true;
    }

}
